package com.maple.util;

import com.maple.user.entity.SysUser;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Generator {

    public static String getMD5Hash(Object object) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            // 假设对象可以转换为字节序列
            md.update(object.toString().getBytes());
            byte[] digest = md.digest();
            // 转换字节到16进制字符串
            StringBuilder hexString = new StringBuilder();
            for (byte b : digest) {
                String hex = Integer.toHexString(0xff & b);
                if (hex.length() == 1) {
                    hexString.append('0');
                }
                hexString.append(hex);
            }
            return hexString.toString();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        // 示例对象
        SysUser sysUser = new SysUser();
        sysUser.setId(1);
        sysUser.setUsername("123456");
        sysUser.setAge(18);

        String md5Hash = getMD5Hash(sysUser);
        System.out.println("MD5 Hash of is: " + md5Hash);


        SysUser sysUser2 = new SysUser();
        sysUser2.setId(1);
        sysUser2.setUsername("123456");
        sysUser2.setAge(18);

        String md5Hash2 = getMD5Hash(sysUser2);
        System.out.println("MD5 Hash of is: " + md5Hash2);

        System.out.println(md5Hash2.equals(md5Hash));
    }

}
